home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Utilities / SortIcons / SortIcons.rexx < prev    next >
OS/2 REXX Batch file  |  2000-05-21  |  7KB  |  272 lines

  1. /* 
  2.    $VER: SortIcons.rexx 1.2 (20.05.00) Copyright (c) Christoph Gutjahr
  3. */
  4.  
  5. /* declare important variables */
  6. h_spacer=16    /* horizontal space between icons */
  7. v_spacer=04    /* vertical space between icons   */
  8. dir.0=1        /* default: "Tool"-Icons to right border */
  9. dir.1=3        /* default: "Disk"-Icons to left border  */
  10. drawer.0=0     /* default: Ignore "Drawer"-Icons when sorting "Tool"-Icons */
  11. drawer.1=0     /* default: Ignore "Drawer"-Icons when sorting "Disk"-Icons */
  12. appicon.0=0    /* default: Ignore "App"-Icons when sorting "Tool"-Icons */
  13. appicon.1=0    /* default: Ignore "App"-Icons when sorting "Disk"-Icons */
  14. loop=0
  15. changes=0
  16.  
  17. /* parse arguments */
  18. PARSE ARG argument
  19. argument=UPPER(argument)
  20. l=WORDS(argument)
  21. IF l>4 THEN CALL ERROR1
  22. DO i=1 TO l
  23.    k=WORD(argument,i)
  24.    j=left(k,length(k)-2)
  25.    m=right(k,1)
  26.    SELECT
  27.       WHEN j="TOOL" THEN DO
  28.          SELECT
  29.             WHEN m="R" THEN dir.0=1
  30.             WHEN m="L" THEN dir.0=3
  31.             WHEN m="T" THEN dir.0=4
  32.             WHEN m="B" THEN dir.0=2
  33.             WHEN m="I" THEN dir.0=0
  34.             OTHERWISE CALL ERROR0
  35.          END
  36.       END
  37.       WHEN j="DISK" THEN DO
  38.          SELECT
  39.             WHEN m="R" THEN dir.1=1
  40.             WHEN m="L" THEN dir.1=3
  41.             WHEN m="T" THEN dir.1=4
  42.             WHEN m="B" THEN dir.1=2
  43.             WHEN m="I" THEN dir.1=0
  44.             OTHERWISE CALL ERROR0
  45.          END
  46.       END
  47.       WHEN j="APPICON" THEN DO
  48.          SELECT
  49.             WHEN m="T" & ~appicon.1 THEN appicon.0=1
  50.             WHEN m="D" & ~appicon.0 THEN appicon.1=1
  51.             OTHERWISE CALL ERROR0
  52.          END
  53.       END
  54.       WHEN j="DRAWER" THEN DO
  55.          SELECT
  56.             WHEN m="T" & ~drawer.1 THEN drawer.0=1
  57.             WHEN m="D" & ~drawer.0 THEN drawer.1=1
  58.             OTHERWISE CALL ERROR0
  59.          END
  60.       END
  61.    END
  62. END
  63.  
  64. ADDRESS workbench
  65. OPTIONS RESULTS
  66. LOCKGUI
  67.  
  68. /* How many icons do we have ? */
  69. GETATTR window.icons.all.count NAME root VAR total
  70.  
  71. DO FOREVER
  72. IF loop=2 THEN leave
  73. IF dir.loop=0 THEN DO
  74.    loop=loop+1
  75.    IF dir.loop=0 | loop=2 THEN LEAVE
  76. END
  77. dir=dir.loop
  78. drawer=drawer.loop
  79. appicon=appicon.loop
  80.  
  81. /*How many ignorable icons do we have ? */
  82. others=0
  83. DO i=0 TO total-1
  84.    GETATTR window.icons.all.i.type NAME root
  85.    SELECT
  86.       WHEN result="APPICON" & appicon THEN NOP
  87.       WHEN result="DRAWER" & drawer THEN NOP
  88.       WHEN result="DISK" & loop THEN NOP
  89.       WHEN result="TOOL" & ~loop THEN NOP
  90.       WHEN result="PROJECT" & ~loop THEN NOP
  91.       OTHERWISE others=others+1
  92.    END
  93. END
  94.  
  95. /* Find the largest height and width of icons */
  96. w=0
  97. h=0
  98. DO i=0 TO total-1
  99.    GETATTR window.icons.all.i NAME root STEM root
  100.    SELECT
  101.       WHEN '"'|| root.type ||'"'='"APPICON"' & ~appicon THEN NOP
  102.       WHEN '"'|| root.type ||'"'='"DRAWER"' & ~drawer THEN NOP
  103.       WHEN '"'|| root.type ||'"'='"DISK"' & ~loop THEN NOP
  104.       WHEN '"'|| root.type ||'"'='"TOOL"' & loop THEN NOP
  105.       WHEN '"'|| root.type ||'"'='"PROJECT"' & loop THEN NOP
  106.       OTHERWISE DO
  107.          IF w<root.width THEN w=root.width
  108.          IF h<root.height THEN h=root.height
  109.          full_name='"'|| root.name ||'"'
  110.          GETATTR application.font.icon.size NAME full_name
  111.          temp=RESULT
  112.          IF w<temp THEN w=temp
  113.       END
  114.    END
  115. END
  116.  
  117. /* Get height of icon font */
  118. GETATTR application.font.icon.height
  119. font_h=result
  120. font_h=font_h+2
  121.  
  122. /* calculate total size of largest icon including name and spacers */
  123. w=w+h_spacer
  124. h=h+font_h+v_spacer
  125.  
  126. /* Get size of workbench */
  127. GETATTR window.width NAME root VAR win_w
  128. GETATTR window.height NAME root VAR win_h
  129.  
  130. /* Calculate grid , EXIT if to many icons to fit.*/
  131. colums=TRUNC(win_w/w)
  132. gridx=w
  133. rows=TRUNC(win_h/h)
  134. gridy=h
  135. IF total>(rows*colums) THEN CALL ERROR2
  136.  
  137. /* Calculate min-/max positions for icons*/
  138. SELECT
  139.    WHEN dir=1 THEN DO
  140.       maxcol=colums-1
  141.       mincol=TRUNC(colums-((total-others)/rows))
  142.       maxrow=rows-1
  143.       minrow=0
  144.    END
  145.    WHEN dir=2 THEN DO
  146.       maxcol=colums-1
  147.       mincol=0
  148.       maxrow=rows-1
  149.       minrow=TRUNC(rows-((total-others)/colums))
  150.    END
  151.    WHEN dir=3 THEN DO
  152.       maxcol=TRUNC((total-others)/rows+0.9999)-1
  153.       mincol=0
  154.       maxrow=rows-1
  155.       minrow=0
  156.    END
  157.    OTHERWISE DO
  158.       maxcol=colums-1
  159.       mincol=0
  160.       maxrow=TRUNC((total-others)/colums+0.9999)-1
  161.       minrow=0
  162.    END
  163. END
  164.  
  165. /* Clear the matrix for vacant grids */
  166. DO i=0 TO colums-1
  167.    DO l=0 TO rows-1
  168.       free.i.l=0
  169.    END
  170. END
  171.  
  172. /* Position Icons to grid */
  173. DO j=0 TO total-1
  174.    GETATTR window.icons.all.j NAME root STEM root
  175.    SELECT
  176.       WHEN '"'|| root.type ||'"'='"APPICON"' & ~appicon THEN NOP
  177.       WHEN '"'|| root.type ||'"'='"DRAWER"' & ~drawer THEN NOP
  178.       WHEN '"'|| root.type ||'"'='"DISK"' & ~loop THEN NOP
  179.       WHEN '"'|| root.type ||'"'='"TOOL"' & loop THEN NOP
  180.       WHEN '"'|| root.type ||'"'='"PROJECT"' & loop THEN NOP
  181.       OTHERWISE DO
  182.          icongridx=TRUNC(root.left/gridx)
  183.          icongridy=TRUNC(root.top/gridy)
  184.          SELECT
  185.             WHEN dir=1 THEN DO  
  186.                IF icongridx<mincol THEN icongridx=mincol
  187.             END
  188.             WHEN dir=2 THEN DO  
  189.                IF icongridy<minrow THEN icongridy=minrow
  190.             END
  191.             WHEN dir=3 THEN DO  
  192.                IF icongridx>maxcol THEN icongridx=maxcol
  193.             END
  194.             OTHERWISE DO  
  195.             IF icongridy>maxrow THEN icongridy=maxrow
  196.             END
  197.          END
  198.          IF free.icongridx.icongridy=1 THEN DO
  199.             DO i=maxcol TO mincol BY -1
  200.                DO l=maxrow TO minrow BY-1
  201.                   IF free.i.l=0 THEN DO
  202.                      icongridx=i
  203.                      icongridy=l
  204.                   END
  205.                END
  206.             END
  207.          END
  208.          IF free.icongridx.icongridy=1 THEN CALL ERROR3
  209.          move_x=TRUNC(icongridx*gridx)
  210.          move_y=TRUNC(icongridy*gridy)
  211.          center_x=TRUNC((gridx-root.width)/2)
  212.          move_x=move_x+center_x
  213.          lock_y=gridy-root.height-font_h-1
  214.          move_y=move_y+lock_y
  215.          full_name='"'||root.name||'"'
  216.          IF move_x~=root.left | move_y ~=root.top THEN changes=1
  217.          ICON WINDOW root NAMES full_name X move_x Y move_y
  218.          free.icongridx.icongridy =1
  219.       END
  220.    END
  221. END
  222. loop=loop+1
  223. END
  224. IF changes=0 THEN DO
  225.    UNLOCKGUI
  226.    EXIT
  227. END
  228. cmdid='req'pragma('id')
  229. address command 'rxset' cmdid '`requestchoice "SortIcon" "Fix icon positions ?" "Yes|No"`'
  230. from_shell=getclip(cmdid)
  231. call setclip(cmdid,'')
  232. IF ~from_shell THEN DO
  233.    UNLOCKGUI
  234.    EXIT
  235. END
  236. /* Save the changes */
  237. ADDRESS WORKBENCH
  238. MENU window ROOT invoke window.snapshot.all
  239. UNLOCKGUI
  240. EXIT
  241.  
  242. /* ------------- The error messages are following below --------------- */
  243.  
  244. /* Error: Unknown argument */
  245. ERROR0:
  246.    ADDRESS COMMAND
  247.    'REQUESTCHOICE >NIL: "SortIcons" "Error: Wrong or misspelled arguments!" "OK"'
  248.    EXIT 10
  249. RETURN
  250.  
  251. /* Error: Too many arguments */
  252. ERROR1:
  253.    ADDRESS COMMAND
  254.    'REQUESTCHOICE >NIL: "SortIcons" "Error: Too many arguments!" "OK"'
  255.    EXIT 10
  256. RETURN
  257. /* Error: Too many Icons to fit in grid ! */
  258. ERROR2:
  259.    ADDRESS COMMAND
  260.    'REQUESTCHOICE >NIL: "SortIcons" "Error: Too many Icons to fit in grid!" "OK"'
  261.    UNLOCKGUI
  262.    EXIT 10
  263. RETURN
  264.  
  265. /* Error: Grid miscalculation ! */
  266. ERROR3:
  267.    ADDRESS COMMAND
  268.    'REQUESTCHOICE >NIL: "SortIcons" "Error: Grid miscalculation!*n*nPlease contact the author." "OK"'
  269.    UNLOCKGUI
  270.    EXIT 10
  271. RETURN
  272.